Skip to content

Conversation

@mnocon
Copy link
Contributor

@mnocon mnocon commented Nov 18, 2025

@github-actions
Copy link

Preview of modified files: no change to preview.

@github-actions
Copy link

code_samples/ change report

Before (on target branch)After (in current PR)

code_samples/discounts/src/Command/ManageDiscountsCommand.php

docs/discounts/discounts_api.md@118:``` php hl_lines="60-67 69-97"
docs/discounts/discounts_api.md@119:[[= include_file('code_samples/discounts/src/Command/ManageDiscountsCommand.php') =]]
docs/discounts/discounts_api.md@120:```

001⫶<?php
002⫶
003⫶declare(strict_types=1);
004⫶
005⫶namespace App\Command;
006⫶
007⫶use DateTimeImmutable;
008⫶use Ibexa\Contracts\Core\Collection\ArrayMap;
009⫶use Ibexa\Contracts\Core\Repository\PermissionResolver;
010⫶use Ibexa\Contracts\Core\Repository\UserService;
011⫶use Ibexa\Contracts\Discounts\DiscountServiceInterface;
012⫶use Ibexa\Contracts\Discounts\Value\DiscountType;
013⫶use Ibexa\Contracts\Discounts\Value\Struct\DiscountCreateStruct;
014⫶use Ibexa\Contracts\Discounts\Value\Struct\DiscountTranslationStruct;
015⫶use Ibexa\Contracts\DiscountsCodes\DiscountCodeServiceInterface;
016⫶use Ibexa\Contracts\DiscountsCodes\Value\Struct\DiscountCodeCreateStruct;
017⫶use Ibexa\Discounts\Value\DiscountCondition\IsInCurrency;
018⫶use Ibexa\Discounts\Value\DiscountCondition\IsInRegions;
019⫶use Ibexa\Discounts\Value\DiscountCondition\IsProductInArray;
020⫶use Ibexa\Discounts\Value\DiscountRule\FixedAmount;
021⫶use Ibexa\DiscountsCodes\Value\DiscountCondition\IsValidDiscountCode;
022⫶use Symfony\Component\Console\Command\Command;
023⫶use Symfony\Component\Console\Input\InputInterface;
024⫶use Symfony\Component\Console\Output\OutputInterface;
025⫶
026⫶final class ManageDiscountsCommand extends Command
027⫶{
028⫶ protected static $defaultName = 'discounts:manage';
029⫶
030⫶ private DiscountServiceInterface $discountService;
031⫶
032⫶ private DiscountCodeServiceInterface $discountCodeService;
033⫶
034⫶ private PermissionResolver $permissionResolver;
035⫶
036⫶ private UserService $userService;
037⫶
038⫶ public function __construct(

code_samples/discounts/src/Command/ManageDiscountsCommand.php

docs/discounts/discounts_api.md@118:``` php hl_lines="60-67 69-97"
docs/discounts/discounts_api.md@119:[[= include_file('code_samples/discounts/src/Command/ManageDiscountsCommand.php') =]]
docs/discounts/discounts_api.md@120:```

001⫶<?php
002⫶
003⫶declare(strict_types=1);
004⫶
005⫶namespace App\Command;
006⫶
007⫶use DateTimeImmutable;
008⫶use Ibexa\Contracts\Core\Collection\ArrayMap;
009⫶use Ibexa\Contracts\Core\Repository\PermissionResolver;
010⫶use Ibexa\Contracts\Core\Repository\UserService;
011⫶use Ibexa\Contracts\Discounts\DiscountServiceInterface;
012⫶use Ibexa\Contracts\Discounts\Value\DiscountType;
013⫶use Ibexa\Contracts\Discounts\Value\Struct\DiscountCreateStruct;
014⫶use Ibexa\Contracts\Discounts\Value\Struct\DiscountTranslationStruct;
015⫶use Ibexa\Contracts\DiscountsCodes\DiscountCodeServiceInterface;
016⫶use Ibexa\Contracts\DiscountsCodes\Value\Struct\DiscountCodeCreateStruct;
017⫶use Ibexa\Discounts\Value\DiscountCondition\IsInCurrency;
018⫶use Ibexa\Discounts\Value\DiscountCondition\IsInRegions;
019⫶use Ibexa\Discounts\Value\DiscountCondition\IsProductInArray;
020⫶use Ibexa\Discounts\Value\DiscountRule\FixedAmount;
021⫶use Ibexa\DiscountsCodes\Value\DiscountCondition\IsValidDiscountCode;
022⫶use Symfony\Component\Console\Command\Command;
023⫶use Symfony\Component\Console\Input\InputInterface;
024⫶use Symfony\Component\Console\Output\OutputInterface;
025⫶
026⫶final class ManageDiscountsCommand extends Command
027⫶{
028⫶ protected static $defaultName = 'discounts:manage';
029⫶
030⫶ private DiscountServiceInterface $discountService;
031⫶
032⫶ private DiscountCodeServiceInterface $discountCodeService;
033⫶
034⫶ private PermissionResolver $permissionResolver;
035⫶
036⫶ private UserService $userService;
037⫶
038⫶ public function __construct(
039⫶        UserService $userSerice,
039⫶        UserService $userService,
040⫶        PermissionResolver $permissionResolver,
041⫶ DiscountServiceInterface $discountService,
042⫶ DiscountCodeServiceInterface $discountCodeService
043⫶ ) {
040⫶        PermissionResolver $permissionResolver,
041⫶ DiscountServiceInterface $discountService,
042⫶ DiscountCodeServiceInterface $discountCodeService
043⫶ ) {
044⫶        $this->userService = $userSerice;
044⫶        $this->userService = $userService;
045⫶        $this->discountService = $discountService;
046⫶ $this->discountCodeService = $discountCodeService;
047⫶ $this->permissionResolver = $permissionResolver;
048⫶
049⫶ parent::__construct();
050⫶ }
051⫶
052⫶ protected function execute(InputInterface $input, OutputInterface $output): int
053⫶ {
054⫶ $this->permissionResolver->setCurrentUserReference(
055⫶ $this->userService->loadUserByLogin('admin')
056⫶ );
057⫶
058⫶ $now = new DateTimeImmutable();
059⫶
060❇️ $discountCodeCreateStruct = new DiscountCodeCreateStruct(
061❇️ 'summer10',
062❇️ 10, // Global usage limit
063❇️ null, // Unlimited usage per customer
064❇️ $this->permissionResolver->getCurrentUserReference()->getUserId(),
065❇️ $now
066❇️ );
067❇️ $discountCode = $this->discountCodeService->createDiscountCode($discountCodeCreateStruct);
068⫶
069❇️ $discountCreateStruct = new DiscountCreateStruct();
070❇️ $discountCreateStruct
071❇️ ->setIdentifier('discount_identifier')
072❇️ ->setType(DiscountType::CART)
073❇️ ->setPriority(10)
074❇️ ->setEnabled(true)
075❇️ ->setUser($this->userService->loadUserByLogin('admin'))
076❇️ ->setRule(new FixedAmount(10))
077❇️ ->setStartDate($now)
078❇️ ->setConditions([
079❇️ new IsInRegions(['germany', 'france']),
080❇️ new IsProductInArray(['product-1', 'product-2']),
081❇️ new IsInCurrency('EUR'),
082❇️ new IsValidDiscountCode(
083❇️ $discountCode->getCode(),
084❇️ $discountCode->getGlobalLimit(),
085❇️ $discountCode->getUsedLimit()
086❇️ ),
087❇️ ])
088❇️ ->setTranslations([
089❇️ new DiscountTranslationStruct('eng-GB', 'Discount name', 'This is a discount description', 'Promotion Label', 'Promotion Description'),
090❇️ new DiscountTranslationStruct('ger-DE', 'Discount name (German)', 'Description (German)', 'Promotion Label (German)', 'Promotion Description (German)'),
091❇️ ])
092❇️ ->setEndDate(null) // Permanent discount
093❇️ ->setCreatedAt($now)
094❇️ ->setUpdatedAt($now)
095❇️ ->setContext(new ArrayMap(['custom_context' => 'custom_value']));
096❇️
097❇️ $this->discountService->createDiscount($discountCreateStruct);
098⫶
099⫶ return Command::SUCCESS;
100⫶ }
101⫶}

045⫶        $this->discountService = $discountService;
046⫶ $this->discountCodeService = $discountCodeService;
047⫶ $this->permissionResolver = $permissionResolver;
048⫶
049⫶ parent::__construct();
050⫶ }
051⫶
052⫶ protected function execute(InputInterface $input, OutputInterface $output): int
053⫶ {
054⫶ $this->permissionResolver->setCurrentUserReference(
055⫶ $this->userService->loadUserByLogin('admin')
056⫶ );
057⫶
058⫶ $now = new DateTimeImmutable();
059⫶
060❇️ $discountCodeCreateStruct = new DiscountCodeCreateStruct(
061❇️ 'summer10',
062❇️ 10, // Global usage limit
063❇️ null, // Unlimited usage per customer
064❇️ $this->permissionResolver->getCurrentUserReference()->getUserId(),
065❇️ $now
066❇️ );
067❇️ $discountCode = $this->discountCodeService->createDiscountCode($discountCodeCreateStruct);
068⫶
069❇️ $discountCreateStruct = new DiscountCreateStruct();
070❇️ $discountCreateStruct
071❇️ ->setIdentifier('discount_identifier')
072❇️ ->setType(DiscountType::CART)
073❇️ ->setPriority(10)
074❇️ ->setEnabled(true)
075❇️ ->setUser($this->userService->loadUserByLogin('admin'))
076❇️ ->setRule(new FixedAmount(10))
077❇️ ->setStartDate($now)
078❇️ ->setConditions([
079❇️ new IsInRegions(['germany', 'france']),
080❇️ new IsProductInArray(['product-1', 'product-2']),
081❇️ new IsInCurrency('EUR'),
082❇️ new IsValidDiscountCode(
083❇️ $discountCode->getCode(),
084❇️ $discountCode->getGlobalLimit(),
085❇️ $discountCode->getUsedLimit()
086❇️ ),
087❇️ ])
088❇️ ->setTranslations([
089❇️ new DiscountTranslationStruct('eng-GB', 'Discount name', 'This is a discount description', 'Promotion Label', 'Promotion Description'),
090❇️ new DiscountTranslationStruct('ger-DE', 'Discount name (German)', 'Description (German)', 'Promotion Label (German)', 'Promotion Description (German)'),
091❇️ ])
092❇️ ->setEndDate(null) // Permanent discount
093❇️ ->setCreatedAt($now)
094❇️ ->setUpdatedAt($now)
095❇️ ->setContext(new ArrayMap(['custom_context' => 'custom_value']));
096❇️
097❇️ $this->discountService->createDiscount($discountCreateStruct);
098⫶
099⫶ return Command::SUCCESS;
100⫶ }
101⫶}

Download colorized diff

@mnocon mnocon marked this pull request as ready for review November 19, 2025 09:10
@mnocon mnocon requested a review from a team November 19, 2025 09:11
@ibexa-workflow-automation-1 ibexa-workflow-automation-1 bot requested review from adriendupuis, dabrt and julitafalcondusza and removed request for a team November 19, 2025 09:11
Copy link
Contributor

@adriendupuis adriendupuis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On 5.0, we silently fixed it during #2864 while moving to constructor promotion 😅

@mnocon mnocon merged commit 52834fc into 4.6 Nov 20, 2025
11 checks passed
@mnocon mnocon deleted the fixed-typo branch November 20, 2025 08:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants